package com.ddwl.goods.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.goods.service.CollectService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.CollectBo;
import com.ddwl.schema.bo.goods.ItemSearchBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.CollectVo;
import com.ddwl.schema.vo.goods.ItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/7/15 9:19
 */
@Api(tags = "收藏服务")
@RequestMapping("collect")
@RestController
public class CollectController extends BaseController {

    @Autowired
    private CollectService collectService;

    @PostMapping("page")
    @ApiOperation("分页查询商品")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<List<ItemVo>> page(@Valid @RequestBody PageBo<ItemSearchBo> pageBo) {
        ItemSearchBo param = Optional.ofNullable(pageBo.getParam()).orElse(new ItemSearchBo());
        param.setUserId(getUserId());
        pageBo.setParam(param);
        return RestVo.ok(collectService.page(pageBo));
    }

    @PostMapping("getCollectPage")
    @ApiOperation("分页查询商品")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<PageVo<CollectVo>> getCollectPage(@Valid @RequestBody PageBo<ItemSearchBo> pageBo) {
        return RestVo.ok(collectService.getCollectPage(pageBo,getUserId()));
    }

    
    @PostMapping
    @ApiOperation("收藏商品")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo collect(@Valid @RequestBody CollectBo bo) {
        bo.setUserId(getUserId());
        collectService.collect(bo);
        return RestVo.ok();
    }
}
